package com.nxk.modules.core.web;

import com.nxk.modules.core.entity.SysMenu;
import com.nxk.modules.core.service.SysMenuService;
import com.nxk.modules.exception.ResultEntity;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/a/core/menu")
public class SysMenuController {


    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 保存数据接口
     *
     * @param sysMenu
     * @return
     */
    @RequiresPermissions("core:menu:edit")
    @PutMapping("/save")
    public ResultEntity save(@Validated SysMenu sysMenu) {
        boolean save = sysMenuService.save(sysMenu);
        return save ? ResultEntity.success(sysMenuService.getById(sysMenu.getId())) : ResultEntity.fail();
    }


    /**
     * 更新数据接口
     * @param sysMenu
     * @return
     */
    @RequiresPermissions("core:menu:edit")
    @PostMapping("/update")
    public ResultEntity update(@Validated SysMenu sysMenu) {
        boolean save = sysMenuService.save(sysMenu);
        return save ? ResultEntity.success(sysMenuService.getById(sysMenu.getId())) : ResultEntity.fail();
    }

    /**
     * 更新数据状态接口
     *
     * @param sysMenu
     * @return
     */
    @RequiresPermissions("core:menu:edit")
    @PostMapping("/updateStatus")
    public ResultEntity updateStatus(SysMenu sysMenu) {
        sysMenuService.updateStatus(sysMenu);
        return ResultEntity.success();
    }

    /**
     * 根据id获取单条数据接口
     *
     * @param sysMenu
     * @return
     */
    @RequiresPermissions("core:menu:view")
    @GetMapping("/get")
    public ResultEntity get(SysMenu sysMenu) {
        return ResultEntity.success(sysMenuService.getById(sysMenu.getId()));
    }


    /**
     * 查询全部菜单数据接口
     *
     * @param sysMenu
     * @return
     */
    @RequiresPermissions("core:menu:view")
    @PostMapping("list")
    public ResultEntity list(SysMenu sysMenu) {
          return ResultEntity.success(sysMenuService.list(sysMenu));
    }


    /**
     * 查询全部菜单权限数据接口
     *
     * @param sysMenu
     * @return
     */
    @RequiresPermissions("core:menu:view")
    @PostMapping("listRole")
    public ResultEntity listRole(SysMenu sysMenu) {
          return ResultEntity.success(sysMenuService.listRole(sysMenu));
    }

    /**
     * 根据id物理删除数据接口
     *
     * @param sysMenu
     * @return
     */
    @RequiresPermissions("core:menu:edit")
    @DeleteMapping("delete")
    public ResultEntity delete(SysMenu sysMenu) {
        boolean delete = sysMenuService.deleteById(sysMenu.getId());
        return delete ? ResultEntity.success() : ResultEntity.fail();
    }


}
